import QtQuick 1.1
import com.nokia.meego 1.0
import "HarmattanUIConstants.js" as Constants

Page {
    id: gestureListPage

    property alias gestureCount: gestureList.gestureCount

    signal modified(int id)
    signal reset(int id)
    signal saved()

    /**
     * Emits modified() signal with the given ID.
     *
     * @param ID The gesture ID.
     */
    function setModified(id)
    {
        gestureListPage.modified(id);
    }

    /**
     * Emits reset() signal with the given ID.
     *
     * @param ID The gesture ID.
     */
    function setReset(id)
    {
        gestureListPage.reset(id);
    }

    /**
     * Emits saved() signal.
     */
    function setSaved()
    {
        gestureListPage.saved();
    }

    // Pages
    FileManagementPage {
        id: fileManagementPage
    }

    EditorPage {
        id: editorPage
    }

    RecognitionPage {
        id: recognitionPage
    }

    // Title containing the name of the current gesture set
    HeaderLabel {
        id: pageTitle
        height: Constants.TITLE_LABEL_HEIGHT
        visible: gestureListPage.gestureCount
        z: 1

        Text {
            anchors.fill: parent
            anchors.margins: Constants.DEFAULT_MARGIN
            verticalAlignment: Text.AlignVCenter
            color: Constants.DEFAULT_TEXT_COLOR
            font.pixelSize: height * 2 / 3
            elide: Text.ElideMiddle
            text: fileManagementPage.filename
        }
    }

    GestureList {
        id: gestureList

        anchors {
            top: pageTitle.visible ? pageTitle.bottom : 0;
            left: parent.left
            right: parent.right
            bottom: parent.bottom
            margins: Constants.DEFAULT_MARGIN
        }

        model: gestureModel

        onContentMovementStarted: gestureList.stopAnimation();
        onContentMovementEnded: gestureList.playAnimation();
        onItemSelected: contextMenu.open();
    }

    Text {
        anchors.centerIn: parent
        wrapMode: Text.WordWrap
        font.pixelSize: 55
        color: "#333333"
        opacity: gestureList.gestureCount ? 0 : 1;
        text: "No gestures"
    }

    // Context menu for gesture list items
    ContextMenu {
        id: contextMenu

        MenuLayout {
            MenuItem {
                text: "Edit"

                onClicked: {
                    var id = gestureList.selectedGestureId;
                    console.debug("Setting gesture ID", id, "to editor.");

                    editorPage.setGestureId(id);
                    pageStack.push(editorPage);
                    gestureList.deselect();
                }
            }
            MenuItem {
                text: "Delete"

                onClicked: {
                    var id = gestureList.selectedGestureId;

                    if (gestureModel.removeGesture(id) &&
                            unprocessedGesturesModel.removeGesture(id)) {
                        console.debug("Removed a gesture with ID", id);
                        helper.setModified(id);
                    }
                    else {
                        console.debug("Failed to delete a gesture with ID", id);
                    }
                }
            }
        }

        onStatusChanged: {
            console.debug("GestureListPage.qml: ContextMenu::onStatusChanged:",
                          status);

            if (status == DialogStatus.Closed) {
                gestureList.deselect();
            }
        }
    }

    // View menu
    Menu {
        id: viewMenu
        visualParent: pageStack

        MenuLayout {
            MenuItem {
                text: "New"
                enabled: gestureList.gestureCount
                visible: enabled

                onClicked: {
                    gestureModel.clear();
                    unprocessedGesturesModel.clear();
                    fileManagementPage.filename = "Untitled";
                }
            }
            MenuItem {
                text: "Load";
                enabled: true

                onClicked: {
                    fileManagementPage.loadMode = true;
                    pageStack.push(fileManagementPage);
                }
            }
            MenuItem {
                text: "Save";
                enabled: (gestureList.gestureCount
                          && helper.modifiedGesturesCount)
                visible: enabled
                onClicked: fileManagementPage.saveFile(fileManagementPage.fullPath);
            }
            MenuItem {
                text: "Save as";
                enabled: gestureList.gestureCount
                visible: enabled

                onClicked: {
                    fileManagementPage.loadMode = false;
                    pageStack.push(fileManagementPage);
                }
            }
            MenuItem {
                text: "About";

                onClicked: {
                    // TODO
                }
            }
        }
    }

    // Tool bar
    tools: ToolBarLayout {
        ToolIcon {
            iconSource: "sketch_icon.png"
            onClicked: {
                pageStack.push(editorPage);
            }
        }
        ToolIcon {
            iconSource: "recognize_icon.png"
            onClicked: {
                pageStack.push(recognitionPage);
            }
        }
        ToolIcon {
            iconId: "toolbar-view-menu"
            onClicked: viewMenu.open();
        }
    }
}

// End of file.
